package com.ops.service;

import com.ops.entity.User;
import com.ops.exception.BusinessException;
import com.ops.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    /**
     * 创建新用户
     * @param user 待创建的用户信息
     * @return 创建成功的用户信息
     * @throws BusinessException 当用户名已存在时抛出异常
     */
    @Transactional
    public User createUser(User user) {
        if (userRepository.existsByUsername(user.getUsername())) {
            throw new BusinessException("USER001", "用户名已存在");
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return userRepository.save(user);
    }
    
    /**
     * 用户登录认证
     * @param username 用户名
     * @param password 密码
     * @return 认证成功的用户信息
     * @throws BusinessException 当用户不存在、密码错误或账号被禁用时抛出异常
     */
    @Transactional(readOnly = true)
    public User authenticate(String username, String password) {
        User user = userRepository.findByUsername(username)
            .orElseThrow(() -> new BusinessException("USER002", "用户不存在"));
            
        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new BusinessException("USER003", "密码错误");
        }
        
        if (!user.getIsActive()) {
            throw new BusinessException("USER004", "账号已被禁用");
        }
        
        // 更新最后登录时间
        user.setLastLoginTime(LocalDateTime.now());
        return userRepository.save(user);
    }
    
    /**
     * 根据用户ID获取用户信息
     * @param id 用户ID
     * @return 用户信息
     * @throws BusinessException 当用户不存在时抛出异常
     */
    @Transactional(readOnly = true)
    public User getUser(Long id) {
        return userRepository.findById(id)
            .orElseThrow(() -> new BusinessException("USER002", "用户不存在"));
    }

    /**
     * 修改用户密码
     * @param username 用户名
     * @param oldPassword 原密码
     * @param newPassword 新密码
     * @throws BusinessException 当用户不存在、原密码错误或新密码与原密码相同时抛出异常
     */
    @Transactional
    public void changePassword(String username, String oldPassword, String newPassword) {
        User user = userRepository.findByUsername(username)
            .orElseThrow(() -> new BusinessException("USER002", "用户不存在"));

        // 验证旧密码
        if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
            throw new BusinessException("USER003", "原密码错误");
        }

        // 验证新密码不能与旧密码相同
        if (passwordEncoder.matches(newPassword, user.getPassword())) {
            throw new BusinessException("USER005", "新密码不能与原密码相同");
        }

        // 更新密码
        user.setPassword(passwordEncoder.encode(newPassword));
        userRepository.save(user);
    }
}